package com.harassment.interception.ui.fragments.interceptRecord;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.harassment.interception.R;
import com.harassment.interception.ui.fragments.HIFragmentManager;

public class InterceptRecordFragment extends Fragment {
    private static final String TAG = "InterceptRecordFragment";
    private ActionBar mActionBar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private AppCompatActivity mAppCompatActivity;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_intercept_record, container, false);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isHidden()) {
            return;
        }
        initActionBar();
        initViewPager();
    }

    private void initViewPager(){
        Log.d(TAG, "initViewPager: 开始");
        Activity activity = getActivity();
        if (activity == null) {
            Log.d(TAG, "initViewPager: 无法获取Activity");
            return;
        }
        if (mViewPager == null) {
            Log.d(TAG, "initViewPager: 初始化viewPager");
            mViewPager = activity.findViewById(R.id.fragment_intercept_record);
        }
        if (mViewPager.getAdapter() == null) {
            mViewPager.setAdapter(new IRViewPagerAdapter(activity));
            mTabLayout.setupWithViewPager(mViewPager);
        }
    }

    private void initActionBar(){
        Log.d(TAG, "initActionBar: 开始");
        Activity activity = getActivity();
        if (activity == null) {
            Log.d(TAG, "initActionBar: 无法获取Activity");
            return;
        }
        AppCompatActivity appCompatActivity = null;
        if (activity instanceof AppCompatActivity){
            appCompatActivity = (AppCompatActivity) activity;
        }else {
            return;
        }

        if (mActionBar == null) {
            Log.d(TAG, "initActionBar: 初始化ActionBar");
            mActionBar = appCompatActivity.getSupportActionBar();
        }

        if (mTabLayout == null) {
            Log.d(TAG, "initActionBar: 初始化TabLayout");
            mTabLayout = (TabLayout) activity.getLayoutInflater().inflate(R.layout.base_tab_layout, null);
        }
        Log.d(TAG, "initActionBar: 配置ActionBar");
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayShowCustomEnabled(true);
        if (mActionBar.getCustomView() != mTabLayout) {
            Log.d(TAG, "initActionBar: 添加自定义布局");
            mActionBar.setIcon(R.drawable.ic_perm_phone_msg_white_24dp);
            mActionBar.setCustomView(mTabLayout,new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER));
        }
    }

    private void rollbackAction(){
        if (mActionBar == null) {
            return;
        }
        Log.d(TAG, "rollbackAction: 还原ActionBar");
        mActionBar.setDisplayShowTitleEnabled(true);
        mActionBar.setDisplayShowCustomEnabled(false);
        mActionBar.setCustomView(null);
        mActionBar.setIcon(null);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        Log.d(TAG, "onHiddenChanged: 开始");
        if (hidden) {
            Log.d(TAG, "onHiddenChanged: 隐藏");
            rollbackAction();
        } else {
            Log.d(TAG, "onHiddenChanged: 显示");
            initActionBar();
            initViewPager();
        }
    }
}
